import {
	computed
} from "vue";

import objectUtils from "@/utils/object/index.js";
import stringUtils from "@/utils/string/index.js";
import LogUtils from "@/utils/log/index.js";
import uniUtils from "@/utils/uni/index.js";
import urlUtils from "@/utils/url/index.js";
import config from '@/config/index.js';

import {
	getMedicalRecordList
} from "@/request/apis/medical-record-post.js";

import {
	useStore
} from '@/store';
	
export default objectUtils.freeze({
	customGo: (item, type) => {
		const store = useStore();
		const patient = computed(() => store.patient.current);
		const patientList = computed(() => store.patient.list);
		
		if (item.activateStatus == 0){
			uni.showToast({
				title:'暂未开放',
				icon:"error"
			})
			return
		}
		if (item.authStatus) {
			if (!patient.value) {
				uniUtils.route.go("/pages-system/login/index");
				return
			}
		}
		switch (item.linkType) {
			case 1:
				uni.navigateToMiniProgram({
					appId: item.miniAppId,
					path: item.linkUrl,
					envVersion: 'release'
				})
				break;
			case 2:
				uniUtils.route.go("/pages-system/web-view/index?url=" + encodeURIComponent(item.linkUrl));
				break;
			case 3:
				uniUtils.route.go(`/pages-system/link-content/index?id=${item.id}&type=${type}`);
				break;
			case 4:
				// 暂时不用
				// specialTreatmentFn(item.linkUrl);
				break;
			case 5:
				if (item.subType === 8) {
					if (patient.value) {
						uniUtils.route.go("/pages-appointment/appointment-type/index")
						// uniUtils.route.go("/pages-system/web-view/index?url=" +
						// 	encodeURIComponent(urlUtils.params.append(item.linkUrl, {
						// 		pkPi: patient.value.pkPi,
						// 		id: patient.value.hisCode,
						// 		idNo: patient.value.idNumber,
						// 		namePi: patient.value.name
						// 	})));
					}else{
						uniUtils.route.go("/pages-system/login/index");
					}
				} else {
					specialTreatmentFn(item.linkUrl);
				}
				break;
			case 6:
				uni.makePhoneCall({
					phoneNumber: item.phone
				});
				break;
			default:
				uniUtils.toast("暂未开放");
				break
		}

		function specialTreatmentFn(path) {
			if (path.endsWith("medical-record-post/order-list") || path.endsWith("medical-record-post/index")) {
				getMedicalRecordList().then((res) => {
					if (res.total > 0) {
						uniUtils.route.go("/pages-hospital-services/medical-record-post/order-list");
					} else {
						uniUtils.route.go("/pages-hospital-services/medical-record-post/index");
					}
				}).catch(() => {
					uniUtils.route.go("/pages-hospital-services/medical-record-post/index");
				});
			} else {
				uniUtils.route.go(path);
			}
		}
	},
	login: () => {
		return new Promise(async (resolve, reject) => {
			uniUtils.login().then((code) => {
				uniUtils.request.post("/app-api/member/auth/social-login", {
          type: 34,
					code: code,
          state: code
				}).then((data) => {
					uni.setStorageSync("system-userinfo", data);
					resolve(true);
				}).catch(() => {
					reject(false);
				});
			}).catch(() => {
				reject(false);
			});
		})
	},
	pay: (config, callback,catchBack) => {
		uniUtils.pay(config).then(() => {
			if (typeof callback === "string") {
				uniUtils.route.go("/pages-system/pay/result?message=" + callback);
			}
			if (typeof callback === "function") {
				callback();
			}
		}).catch(e=>{
			if(catchBack){
				catchBack()
			}
		});
	},
	getDict: function(key, appendArr, wait = true) {
		let systemDict = uni.getStorageSync("system-dictionary");
		if (systemDict && systemDict[key]) {
			if (appendArr) {
				return appendArr.concat(systemDict[key]);
			} else {
				return systemDict[key];
			}
		} else {
			return [];
		}
	},
	getConfig: function(key, isNumber = true) {
		let systemConfig = uni.getStorageSync("system-hospital-config");
		if (systemConfig && systemConfig[key]) {
			if(isNumber){
				return Number(systemConfig[key]);
			}else{
				return systemConfig[key];
			}
		} else {
			if(isNumber){
				return 0;
			}else{
				return "";
			}
		}
	},
	share: function(pages, title){
		const page = pages[pages.length - 1];
		let currentTitle = __wxConfig?.page[page.route + ".html"].window.navigationBarTitleText;
		let currentPath = page.$page.fullPath;
		if(!config.hospitalId){
			let currentPathParams = urlUtils.params.get(currentPath);
			if(!currentPathParams.hospitalId){
				currentPath = urlUtils.params.append(currentPath, {
					hospitalId: uni.getStorageSync("hospitalId") || ""
				});
			}
		}
		return {
			title: title || currentTitle,
			path: currentPath,
			desc: "",
			imageUrl: ""
		}
	},
	isIos: function() {
		// #ifdef MP-WEIXIN
		return wx.getSystemInfoSync().system.startsWith("iOS");
		// #endif
		return false;
	}
});